<?php
require_once ('../../simpletest/autorun.php');

class TestTemp extends UnitTestCase {
    
    function findFilesStartWith($targetDir,$needle){
        
        $files = array();
        if ($handle = opendir($targetDir)) {
        
            /* This is the correct way to loop over the directory. */
        
            while (false !== ($file = readdir($handle))) {
                if(false !== strpos($file, $needle)){
                    $files[] = $file;
                }        
            }
        
            closedir($handle);
        }
        return $files;
    }
    
    function renameFile($filename){
        //remove CopyOf and 
        $filename = str_replace("CopyOf", "", $filename);
        //replace get with process
        $filename = str_replace("get", "process", $filename);
        
        return $filename;
    }    
    
    //refer to http://www.simpletest.org/en/unit_test_documentation.html
    function testFind(){ 
        $targetDir = 'F:\wamp\www\o-my-friends\test';
        $needle = "1234";
        $result = self::findFilesStartWith($needle);
        
        $this->assertNotNull($result);
        $this->assertTrue(count($result) == 2);
    }
    
    function testRename(){

        //Dummy Filename
        $filename1 = "CopyOfTestFunc_getTEST1.php";
        $filename2 = "CopyOfTestFunc_getTEST2.php";
        $newname1 = self::renameFile($filename1);
        $this->assertEqual($newname1, "TestFunc_processTEST1.php");
        $newname2 = self::renameFile($filename2);
        $this->assertEqual($newname2, "TestFunc_processTEST2.php");
        
    }
    
    function testGo(){
        $targetDir = 'F:\wamp\www\o-my-friends\test';
        $needle = "CopyOfTestFunc";
        $files = self::findFilesStartWith($targetDir, $needle);
        foreach($files as $file){
            $newfilename = self::renameFile($file);
            rename($targetDir."\\".$file, $targetDir."\\".$newfilename);
        }
        $result = self::findFilesStartWith($targetDir, $needle);
        $this->assertTrue(empty($result));
    }
}






?>
